package nnet2;

import jargs.gnu.CmdLineParser;
import jargs.gnu.CmdLineParser.Option;

import java.util.ArrayList;
import java.util.Iterator;

import nnet2.learning.FileLearning;
import nnet2.net.RecursiveMultiLayerPerceptron;

public class ConsoleMain {

	/**
	 * @param args
	 */

	static ArrayList<String> optionHelpStrings = new ArrayList<String>();

	public static Option addHelp(Option option, String helpString) {
		optionHelpStrings.add(" -" + option.shortForm() + "/--"
				+ option.longForm() + ": " + helpString);
		return option;
	}

	public static void printUsage() {
		System.err.println("usage: prog [options]");
		for (Iterator<String> i = optionHelpStrings.iterator(); i.hasNext();) {
			System.err.println(i.next());
		}
	}

	public static void main(String[] args) {

		CmdLineParser clp = new CmdLineParser();
		CmdLineParser.Option vi = addHelp(clp.addBooleanOption('v', "visual"),
				"Visual training");
		CmdLineParser.Option lr = addHelp(clp.addDoubleOption('l',
				"learningRate"), "Learning rate");
		CmdLineParser.Option hidd = addHelp(clp.addIntegerOption('h',
				"hiddenSize"), "Hidden layer size");
		CmdLineParser.Option rec = addHelp(clp.addIntegerOption('r',
				"recursion"), "Recursive layers size");
		CmdLineParser.Option in = addHelp(clp.addStringOption('i', "in"),
				"Input file");

		try {
			clp.parse(args);
		} catch (CmdLineParser.OptionException e) {
			System.err.println(e.getMessage());
			printUsage();
			System.exit(2);
		}
		Boolean visual = (Boolean) clp.getOptionValue(vi, Boolean.FALSE);
		Double learningRate = (Double) clp.getOptionValue(lr, .8d);
		Integer hiddenSize = (Integer) clp.getOptionValue(hidd, 10);
		Integer recursion = (Integer) clp.getOptionValue(rec, 5);
		String input = (String) clp.getOptionValue(in);
		
		if(input==null){
			System.err.println("No input specified, will exit.");
			System.exit(2);
		}

		System.out.println("Visual: "+visual);
		System.out.println("Learning rate: "+learningRate);
		System.out.println("Hidden layer size: "+hiddenSize);
		System.out.println("Recursion length: "+recursion);
		System.out.println("Input file: "+input);

		// RMLP net = new RMLP(1, hiddenSize, 1, recursion, recursion);
		// net.setLearningRate(learningRate);
		// net.setMomentum(momentum);

		RecursiveMultiLayerPerceptron rmlp = new RecursiveMultiLayerPerceptron(
				recursion, recursion, hiddenSize, true);
		rmlp.setLeaningRate(learningRate);
		// StackLearning l = new StackLearning(net, input);
		FileLearning l = new FileLearning(rmlp, input);
		l.traindAndLearn(visual);
		// if (visual)
		// l.visualTrain(iters);
		// else
		// l.train(iters);
		// throw new RuntimeException("How to output data?");
		// System.out.format("Błąd:\t%+e\n",l.test());
		// l.visualRep();
	}
}
